from django.shortcuts import render
from rest_framework.pagination import PageNumberPagination
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.status import HTTP_201_CREATED, HTTP_200_OK
from rest_framework.viewsets import ModelViewSet, GenericViewSet
from rest_framework.mixins import CreateModelMixin, ListModelMixin, DestroyModelMixin
from django.db.models import F
from community.models import Article, Comment
from community.serializers import ArticleSerializer, CommentSerializer, CommentHandlerSerializer
from utils.customPermissions import auto_user


class ArticlePageNumberPagination(PageNumberPagination):
    page_size = 5  # 默认每页显示5条
    page_size_query_param = 'size'  # 每页可展示数据的最大条数的字段
    max_page_size = 100  # 每页可展示数据的最大条数
    # page_size：每页数目
    # page_query_param：前端发送的页数关键字名，默认为”page”
    # page_size_query_param：前端发送的每页数目关键字名，默认为None
    # max_page_size：前端最多能设置的每页数量


class ArticleViewSet(ModelViewSet):
    queryset = Article.objects.filter(is_delete=False, status=1)  # 获取已发布未删除的文章
    serializer_class = ArticleSerializer
    permission_classes = [IsAuthenticated]  # drf内置登录权限，登录用户才能进入
    pagination_class = ArticlePageNumberPagination

    @auto_user  # 把提交用户自动换成登录用户
    def create(self, request, *args, **kwargs):
        serializer = self.get_serializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=HTTP_201_CREATED)

    @auto_user  # 把提交用户自动换成登录用户
    def update(self, request, *args, **kwargs):
        # def update(self, request, pk):
        # if not Article.objects.filter(pk=pk, user=request.user):
        # 非登录用户不能修改
        if not Article.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '您没有权限进行修改'})
        article = Article.objects.get(id=int(kwargs['pk']), user=request.user)
        serializer = self.get_serializer(article, data=request.data)
        serializer.is_valid(raise_exception=True)
        serializer.save()
        return Response(serializer.data, status=HTTP_200_OK)

    def destroy(self, request, *args, **kwargs):
        # 非登录用户不能删除
        if not Article.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '您没有权限进行删除'})
        return ModelViewSet.destroy(self, request, *args, **kwargs)

    def retrieve(self, request, *args, **kwargs):
        # F()获取符合条件某个字段对应的值
        Article.objects.filter(id=int(kwargs['pk'])).update(page_view=F('page_view') + 1)
        return ModelViewSet.retrieve(self, request, *args, **kwargs)


class CommentViewSet(GenericViewSet, CreateModelMixin, ListModelMixin, DestroyModelMixin):
    queryset = Comment.objects.filter(level=1)
    serializer_class = CommentHandlerSerializer
    permission_classes = [IsAuthenticated]  # drf内置登录权限，登录用户才能进入

    @auto_user  # 把提交用户自动换成登录用户
    def create(self, request, *args, **kwargs):
        return CreateModelMixin.create(self, request, *args, **kwargs)

    def destroy(self, request, *args, **kwargs):
        # 非登录用户不能删除
        if not Comment.objects.filter(id=int(kwargs['pk']), user=request.user):
            return Response({'msg': '您没有权限进行删除'})
        return DestroyModelMixin.destroy(self, request, *args, **kwargs)
